﻿/*
   Original author: Dandre Jansen van Vuuren

   This wrapper code is intended to be included in the project that needs to use it.
   In other words, there is no project references made, the source file is included in the project.
   This file is not governed by the MS-PL license but carries the following disclaimer if used
   in your personal/commercial/open-source/etc project:

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Diagnostics;
using System.Globalization;
using System.Text;

namespace RenewalProjects.Libraries.SharedCode
{
	/// <summary>
	/// A logging wrapper class that uses the .NET tracing mechanism for writing out logs.
	/// You can write a trace listener that invokes your logging framework of choice.
	/// </summary>
	public static class Logging
	{
		/// <summary>
		/// Write out message for compilations using DEBUG.
		/// </summary>
		[Conditional("DEBUG")]
		public static void Debug(string message)
		{
			System.Diagnostics.Debug.WriteLine(message);
		}

		/// <summary>
		/// Write out message for compilations using DEBUG.
		/// </summary>
		[Conditional("DEBUG")]
		public static void Debug(string format, params object[] args)
		{
			System.Diagnostics.Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, format, args));
		}

		/// <summary>
		/// Write out a message for information logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Info(string message)
		{
			System.Diagnostics.Trace.TraceInformation(message);
		}

		/// <summary>
		/// Write out a message for information logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Info(string format, params object[] args)
		{
			System.Diagnostics.Trace.TraceInformation(format, args);
		}

		/// <summary>
		/// Write out a message for warning logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Warn(string message)
		{
			System.Diagnostics.Trace.TraceWarning(message);
		}

		/// <summary>
		/// Write out a message for warning logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Warn(string format, params object[] args)
		{
			System.Diagnostics.Trace.TraceWarning(format, args);
		}

		/// <summary>
		/// Write out a message for error logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Error(string message)
		{
			System.Diagnostics.Trace.TraceError(message);
		}

		/// <summary>
		/// Write out a message and exception message for error logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Error(Exception exception, string message)
		{
			System.Diagnostics.Trace.TraceError(message + Environment.NewLine 
#if LOG_FULL_EXCEPTION
+ ExceptionUtilities.FormatExceptionDepth(exception, 1, true)
#else
+ exception.GetBaseException().ToString()
#endif
			                                   );
		}

		/// <summary>
		/// Write out a message for error logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Error(string format, params object[] args)
		{
			System.Diagnostics.Trace.TraceError(format, args);
		}

		/// <summary>
		/// Write out a message and exception message for error logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Error(Exception exception, string format, params object[] args)
		{
			System.Diagnostics.Trace.TraceError(format + Environment.NewLine
#if LOG_FULL_EXCEPTION			                                    
+ ExceptionUtilities.FormatExceptionDepth(exception, 1, true)
#else
+ exception.GetBaseException().ToString()
#endif
				, args);
		}

		/// <summary>
		/// Write out a message for fatal logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Fatal(string message)
		{
			System.Diagnostics.Trace.Fail(message);
		}

		/// <summary>
		/// Write out a message and exception message for fatal logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Fatal(Exception exception, string message)
		{
			System.Diagnostics.Trace.Fail(message + Environment.NewLine
#if LOG_FULL_EXCEPTION			                              
+ ExceptionUtilities.FormatExceptionDepth(exception, 1, true)
#else
+ exception.GetBaseException().ToString()
#endif
			                              
			);
		}

		/// <summary>
		/// Write out a message for fatal logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Fatal(string format, params object[] args)
		{
			System.Diagnostics.Trace.Fail(String.Format(CultureInfo.CurrentCulture, format, args));
		}

		/// <summary>
		/// Write out a message and exception message for fatal logging using TRACE.
		/// </summary>
		[Conditional("TRACE")]
		public static void Fatal(Exception exception, string format, params object[] args)
		{
			System.Diagnostics.Trace.Fail(String.Format(CultureInfo.CurrentCulture, format + Environment.NewLine
#if LOG_FULL_EXCEPTION			                                            
+ ExceptionUtilities.FormatExceptionDepth(exception, 1, true)
#else
+ exception.GetBaseException().ToString()
#endif
				, args));
		}
	}
}